﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Text;
using System.Security;
using System.Reflection;

namespace Job.Core
{
    /// <summary>
    /// 获取输入信息类
    /// </summary>
    public class GetHelper
    {
        /// <summary>
        /// 获取对象的值
        /// </summary>
        /// <typeparam name="T">数值型类型 Int32、String</typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T GetVal<T>(object obj)
        {

            if (obj == null || obj == DBNull.Value)
                return default(T);
            try
            {
                return (T)Convert.ChangeType(obj, typeof(T));
            }
            catch
            {
                return default(T);
            }
        }

        /// <summary>
        /// 获取对象的值
        /// </summary>
        /// <typeparam name="T">数值型类型 Int32、String</typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T GetVal<T>(object obj, T defaultVal)
        {
            if (obj == null || obj == DBNull.Value)
                return defaultVal;
            try
            {
                return (T)Convert.ChangeType(obj, typeof(T));
            }
            catch
            {
                return defaultVal;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static T GetValQuery<T>(string name, T defaultVal)
        {
            return GetVal<T>(HttpContext.Current.Request.QueryString[name], defaultVal);
        }

        /// <summary>
        /// 
        /// </summary>
        public static T GetValForm<T>(string name, T defaultVal)
        {
            return GetVal<T>(HttpContext.Current.Request.Form[name], defaultVal);
        }


        /// <summary>
        /// 获取Bool值
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool GetBool(string text)
        {
            if (string.IsNullOrEmpty(text)) return false;
            bool iOut = false;
            if (!String.IsNullOrEmpty(text.Trim()))
                bool.TryParse(text, out iOut);
            return iOut;
        }

        /// <summary>
        /// 获取short值
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static short GetShortInt(string text)
        {
            if (string.IsNullOrEmpty(text)) return 0;
            text = text.Trim();
            short outResult;
            Int16.TryParse(text, out outResult);
            return outResult;
        }

        /// <summary>
        /// 获取int值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int GetInt(object obj)
        {
            if (obj == null) return 0;
            return GetInt(obj.ToString());
        }

        /// <summary>
        /// 获取int值
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static int GetInt(string text)
        {
            if (string.IsNullOrEmpty(text)) return 0;
            text = text.Trim();
            int outResult;
            
            Int32.TryParse(text, out outResult);
            return outResult;
        }

        /// <summary>
        /// 获取long值
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static long GetLongInt(string text)
        {
            if (string.IsNullOrEmpty(text)) return 0;
            text = text.Trim();
            long outResult;
            Int64.TryParse(text, out outResult);
            return outResult;
        }

        /// <summary>
        /// 获取decimal值
        /// </summary>
        public static decimal GetDecimal(string text)
        {
            if (string.IsNullOrEmpty(text)) return 0;
            text = text.Trim();
            decimal outResult;
            Decimal.TryParse(text, out outResult);
            return outResult;
        }

        /// <summary>
        /// 获取DateTime值
        /// </summary>
        public static DateTime GetDateTime(string text)
        {
            if (string.IsNullOrEmpty(text)) return DateTime.MinValue;
            text = text.Trim();
            DateTime outResult;
            DateTime.TryParse(text, out outResult);
            return outResult;
        }
                
        /// <summary>
        /// 获取string值  替换 ' => ''
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string GetStr(object obj)
        {
            return GetStr(obj, false);
        }
        
        /// <summary>
        /// 获取string值
        /// </summary>
        /// <param name="text"></param>
        /// <param name="replace">是否替换 ' => ''</param>
        /// <returns></returns>
        public static string GetStr(object obj, bool replace)
        {
            if (obj == null || obj == DBNull.Value)
                return string.Empty;
            string text = obj.ToString();

            text = string.IsNullOrEmpty(text) ? string.Empty : text.Trim();
            return replace == true ? text.Replace("'", "''") : text;
        }

        public static string GetStrByDate(object dateObject)
        {
            return GetStrByDate(dateObject, "yyyy-MM-dd");
        }
        /// <summary>
        /// 输出时间格式
        /// </summary>
        /// <param name="dateObject">时间</param>
        /// <param name="dateType">输出格式 "yyyy-MM-dd" </param>
        /// <returns></returns>
        public static string GetStrByDate(object dateObject, string dateType)
        {
            if (dateObject == null || dateObject == System.DBNull.Value) 
                return string.Empty;
            try
            {
                Convert.ToDateTime(dateObject);
            }
            catch (Exception)
            {
                return dateObject.ToString();
            }
            return Convert.ToDateTime(dateObject).ToString(dateType);
        }

        /// <summary>
        /// 获取服务器日期时间函数
        /// </summary>
        /// <returns></returns>        
        public static string GetSysDateTime()
        {
            return System.DateTime.Now.ToString("yyyyMMdd");
        }
        
        /// <summary>
        /// 获取服务器日期时间函数
        /// yyyy
        /// yyyy-MM-dd ...
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetSysDateTime(string str)
        {
            return System.DateTime.Now.ToString(str);
        }
        
        /// <summary>
        /// 获取应用程序的程序路径
        /// </summary>
        public static string WebApplicationRootPath()
        {
            string path = System.Web.HttpContext.Current.Request.ApplicationPath;
            if (path.Length > 1) path = path + "/";
            return path;
        }


        /// <summary>
        /// 获取0-9 a-z的随机字符串
        /// </summary>
        /// <param name="intCount">n位随机字符串</param>
        /// <returns></returns>
        public static string GetRandomStr(int intCount)
        {
            string strReturn = "";
            if (intCount > 0)
            {
                Random random = new Random();
                string[] strArray = new string[36] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

                for (int i = 0; i < intCount; i++)
                {
                    strReturn += strArray[random.Next(0, 35)];
                }
            }
            return strReturn;
        }


        /// <summary>
        /// 获取0-9 a-z的随机数
        /// </summary>
        /// <param name="intCount">n位随机随机数</param>
        /// <returns></returns>
        public static string GetRandomNumber(int intCount)
        {
            string strReturn = "";
            if (intCount > 0)
            {
                Random random = new Random();
                string[] strArray = new string[10] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

                for (int i = 0; i < intCount; i++)
                {
                    strReturn += strArray[random.Next(0, 10)];
                }
            }
            return strReturn;
        }


    }
}
